package com.team24.hotel.controller;

import com.team24.hotel.domain.ParentPermission;
import com.team24.hotel.domain.Permission;
import com.team24.hotel.service.ParentPermissionService;
import com.team24.hotel.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;
    @Autowired
    private ParentPermissionService parentPermissionService;

    @RequestMapping("/getAll")
    public String getAllPermission(Model model){
        List<Permission> permissionList=permissionService.findAll();
        model.addAttribute("permissionList",permissionList);
        return "permissionList";
    }

    @RequestMapping("/add")
    public String addPermission(Model model){
        List<ParentPermission> parentPermissionList=parentPermissionService.findAll();
        model.addAttribute("permission",new Permission());
        model.addAttribute("parentPermissionList",parentPermissionList);
        return "permissionAdd";
    }

    @RequestMapping("/edit")
    public String editInfoPrint(String id,Model model){
        Permission permission=permissionService.findByPrimaryKey(Integer.parseInt(id));
        List<ParentPermission> parentPermissionList=parentPermissionService.findAll();
        model.addAttribute("permission",permission);
        model.addAttribute("parentPermissionList",parentPermissionList);
        return "permissionAdd";
    }

    @RequestMapping("/save")
    public String savePermission(Permission permission){
        permissionService.saveOrUpdate(permission);
        return "redirect:/permission/getAll";
    }

    @RequestMapping("/delete")
    public String deletePermission(String id){
        int del;
        del=permissionService.deletePermission(Integer.parseInt(id));
        if(del==1) return "redirect:/permission/getAll";
        else return "404";
    }

}
